(function (window) {
    function main(selector) {
        if (typeof selector === 'object') {
            if (selector.el) {
                this.el = selector.el;
            } else {
                this.el = selector;
            }
        } else {
            let elements = document.querySelectorAll(selector);
            if (elements.length === 1) {
                this.el = elements[0];
            } else {
                this.el = elements;
            }
        }
    }

    main.prototype.child = function (selector) {
        let child = this.el.querySelectorAll(selector);
        if (child.length === 1) {
            this.el = child[0];
        } else {
            this.el = child;
        }
        return this;
    }

    main.prototype.eq = function (index) {
        this.el = this.el[index];
        return this;
    }
    main.prototype.addClass = function (className) {
        this.el.classList.add(className);
        return this;
    }
    main.prototype.removeClass = function (className) {
        this.el.classList.remove(className);
        return this;
    }
    main.prototype.html = function (content) {
        if (content) {
            this.el.innerHTML = content;
            return this;
        } else {
            return this.el.innerHTML;
        }
    }
    main.prototype.create = function (tagName) {
        this.el = document.createElement(tagName);
        return this;
    }
    main.prototype.append = function (element) {
        if (element.el) {
            this.el.appendChild(element.el);
        } else {
            this.el.appendChild(element);
        }
        return this;
    }
    main.prototype.value = function(){
        return this.el.value;
    }
    main.prototype.attr = function (attrName, value) {
        if (value || value === 0) {
            let attr = document.createAttribute(attrName);
            attr.value = value;
            this.el.setAttributeNode(attr);
            return this;
        } else {
            return this.el.getAttribute(attrName);
        }
    }
    main.prototype.on = function (event, fun) {
        this.el.addEventListener(event, fun)
        return this;
    }
    main.prototype.click = function (fun) {
        this.el.onclick = fun;
        return this;
    }
    main.prototype.remove = function(item){
        if(item.el){
            this.el.remove(item.el);
        }else{
            this.el.remove(item)
        }
        return this;
    }
    main.prototype.show = function(){
        this.el.style.display = 'block';
        return this;
    }
    main.prototype.hidden = function(){
        this.el.style .display = 'none';
        return this;
    }

    main.prototype.ajax = function (obj) {
        if (obj.async === undefined) {
            obj.async = true;
        }
        const xhr = new XMLHttpRequest();
        if (!obj.method) {
            obj.method = "GET";
        }
        let param = "";
        for (key in obj.data) {
            if (param == "") {
                param += `${key}=${obj.data[key]}`;
            } else {
                param += `&${key}=${obj.data[key]}`;
            }
        }
        var url = obj.url;
        if (obj.method.toLowerCase() === "get") {
            url += param;
        }
        if (obj.traditional === false) {
            obj.traditional = false;
        } else {
            obj.traditional = true;
        }
        if (obj.traditional) {
            param = {};
            for (key in obj.data) {
                param[key] = obj.data[key];
            }
            param = JSON.stringify(param)
        }
        xhr.open(obj.method, url, obj.async);
        setHeaders();
        if (obj.method.toLowerCase() === "get") {
            xhr.send();
        } else {
            setHeaders("Content-type", "application/json");
            xhr.send(param);
        }
        if (obj.async) {
            xhr.onload = () => {
                if (xhr.status === 200) {
                    if (obj.success) {
                        obj.success(JSON.parse(xhr.responseText));
                    }
                } else {
                    if (obj.fail) {
                        obj.fail(xhr.status);
                    }
                    new Error(xhr.statusText);
                }
            };
            xhr.onerror = () => {
                if (obj.fail) {
                    obj.fail(xhr.status);
                }
                new Error(xhr.statusText);
            };
        } else {
            if (xhr.readyState === 4) {
                if (xhr.status === 200) {
                    if (obj.success) {
                        obj.success(JSON.parse(xhr.responseText));
                    }
                } else {
                    if (obj.fail) {
                        obj.fail(xhr.status);
                    }
                    new Error(xhr.statusText);
                }
            } else {
                if (obj.fail) {
                    obj.fail(xhr.readyState);
                }
                new Error(xhr.readyState);
            }
        }

        function setHeaders(title, value) {
            if (title && value) {
                xhr.setRequestHeader(title, value);
            } else {
                for (head in obj.headers) {
                    xhr.setRequestHeader(head, obj.headers[head]);
                }
            }
        }
    }

    function instantiation(selector) {
        return new main(selector);
    }
    window.$ = instantiation;
}(window))